package cn.ytr.crm.query;

import java.util.Collections;
import java.util.List;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

//封装页面中需要的数据(分页结果对象)
@Getter
@ToString
@NoArgsConstructor
//@Component
public class PageResult {

	//页面传递的数据
	private Integer currentPage;
	private Integer pageSize;
	//sql查询出来的数据
	private List<?> list;
	private Integer totalCount;
	//计算出来的数据
	private Integer prevPage;
	private Integer nextPage;
	private Integer totalPage;
	
	//当总条数为0时使用
	public PageResult(Integer pageSize){
		this(1, 0, Collections.EMPTY_LIST, pageSize);
	}
	
	public PageResult(Integer currentPage, Integer totalCount, List<?> list, Integer pageSize) {
		this.currentPage = currentPage;
		this.pageSize = pageSize;
		this.list = list;
		this.totalCount = totalCount;
		
		//计算
		this.totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
		this.prevPage = currentPage > 1 ? currentPage - 1 : 1;
		this.nextPage = currentPage < totalPage ? currentPage+1 : totalPage;
	}
	
	//交给 Spring 管理后的参数
	//当总条数为0时使用
	public void pullInfo(Integer pageSize){
		pullInfo(1, 0, Collections.EMPTY_LIST, pageSize);
	}
	public void pullInfo(Integer currentPage, Integer totalCount, List<?> list, Integer pageSize) {
		this.currentPage = currentPage;
		this.pageSize = pageSize;
		this.list = list;
		this.totalCount = totalCount;
		
		//计算
		this.totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
		this.prevPage = currentPage > 1 ? currentPage - 1 : 1;
		this.nextPage = currentPage < totalPage ? currentPage+1 : totalPage;
	}
	
	
	
}
